import graphene
from graphene_django.types import DjangoObjectType

from apps.project.models import Project
from apps.plan.models import Plan


class ProjectT(DjangoObjectType):
    class Meta:
        model = Project


class PlanT(DjangoObjectType):
    class Meta:
        model = Plan


#  定义查询
class Query(graphene.ObjectType):
    all_projects = graphene.List(ProjectT)
    all_plans = graphene.List(PlanT, project=graphene.Int())

    @graphene.resolve_only_args
    def resolve_all_projects(self, **kwargs):
        return Project.objects.all()

    @graphene.resolve_only_args
    def resolve_all_plans(self, project=None):
        # print('args: {}'.format(args))
        print('project: {}'.format(project))
        if project:
            return Plan.objects.filter(project=project)
        else:
            return Plan.objects.all()


schema = graphene.Schema(query=Query)
